﻿//
// *************************************************************************************
// WP-Framework
//
// Developed by 
// Lukas Kretschmar (lkretschmar.dev@bluewin.ch)
//
// Switzerland, Copyright (c) 2012 lkretschmar
// This content is released under the MIT License (http://opensource.org/licenses/MIT).
//     
// *************************************************************************************
//
//
			

using System;
using System.Collections.Generic;
using Expression = System.Linq.Expressions.Expression;
using KretschIT.WP_Fx.EasyMoq.Calls.Arguments;
using KretschIT.WP_Fx.EasyMoq.Matchers;

namespace KretschIT.WP_Fx.EasyMoq.Extensions
{
    internal static class ExpressionExtensions
    {
        private static readonly IMatcherFactory matcherFactory = new DefaultMatcherFactory();

        public static IEnumerable<IArgument> ToArguments(this IEnumerable<Expression> collection)
        {
            return CollectionOf<IArgument>(collection, e => new Argument(e));
        }

        public static IEnumerable<IMatcher> ToMatchers(this IEnumerable<Expression> collection)
        {
            return CollectionOf<IMatcher>(collection, e => matcherFactory.Create(e));
        }

        private static IEnumerable<T> CollectionOf<T>(IEnumerable<Expression> collection, Func<Expression, T> create)
        {
            var list = new List<T>();
            if (collection != null)
            {
                Fill(list, collection, create);
            }
            return list;
        }

        private static void Fill<T>(List<T> arguments, IEnumerable<Expression> collection, Func<Expression, T> create)
        {
            foreach (var expr in collection)
            {
                arguments.Add(create(expr));
            }
        }
    }
}
